<?php
/**
 * 主线流程专营店信息
 * @author qianxun
 *
 */
class spreadProcStoreModel extends MY_Model{
    function spreadProcStoreModel(){
        parent::MY_Model();
        $this->table = 'SpreadProcStore';
    }
    /**
     * 包装数据
     * {@inheritDoc}
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        $storeIds = array();
        foreach ($list as $v){
            $storeIds[] = (int)$v['storeId'];
        }
        $this->CI->load->model('spreadStoreModel');
        $storeByIdList = $this->CI->spreadStoreModel->getListByStoreIds($storeIds);
        
        foreach ($list as $k => $v){
            $v['storeTypeStr'] = isset($storeByIdList[$v['storeId']]) ? $storeByIdList[$v['storeId']]['storeTypeStr'] : '协办店';

            $list[$k] = $v;
        }
        return $list;
    }
    /**
     * 判断专营店是否在主线流程中
     * @param number $procId
     * @param number $storeId
     * @return boolean
     */
    function isInProcStore($procId, $storeId){
        $findData = $this->getData(array('storeId'=>$storeId, 'procId'=>$procId));
        return $findData ? true : false;
    }
    /**
     * 获取可选专营店列表
     * @param number $storeId
     * @param number $procId
     * @return array
     */
    function getStoreList($storeId, $procId = 0){
        $this->CI->load->model('spreadStoreModel');
        $storeList = $this->CI->spreadStoreModel->getXiebanStoreList($storeId);
        $storeIds = array_keys($storeList);
        //已选专营店列表
        $procStoreList = $this->getList(array('procId'=>$procId));
        //编辑时
        if ($procStoreList){
            $checkedStoreList = array();
            foreach ($procStoreList as $v){
                $checkedStoreList[(int)$v['storeId']] = $v;
            }
            //初始化专营店列表
            foreach ($storeList as $k => $v){
                $v['applyMoney'] = isset($checkedStoreList[$k]) ? $checkedStoreList[$k]['applyMoney'] : 0;
                $v['isChecked'] = isset($checkedStoreList[$k]) ? true : false;
                $storeList[$k] = $v;
            }
        }else{//新增时
            $filterStoreIds = array();
            //已经被其他专营店选中的专营店自动过滤掉
            $haveFindStoreList = $this->getList(array('storeId'=>array('in',$storeIds)), NULL, NULL, 'storeId');
            if ($haveFindStoreList){
                foreach ($haveFindStoreList as $v){
                    $filterStoreIds[] = (int)$v['storeId'];
                }
                foreach ($storeList as $k => $v){
                    if (in_array($k, $filterStoreIds)){
                        unset($storeList[$k]);
                    }
                }
            }
            //初始化变量值
            foreach ($storeList as $k => $v){
                $v['applyMoney'] = 0;
                $v['isChecked'] = $k == $storeId ? true : false;
                $storeList[$k] = $v;
            }
        }
        //初始化年度额度
        $this->CI->load->model('importMoneyModel');
        $yearMoneyList = $this->CI->importMoneyModel->getStoresMoney(51, array_keys($storeList));
        foreach ($storeList as $k => $v){
            $v['yearMoney'] = isset($yearMoneyList[$k]) ? $yearMoneyList[$k] : 0;
            $storeList[$k] = $v;
        }
        return $storeList;
    }
    /**
     * 获取专营店年度申请金额
     * @param number $storeId
     * @param number $procId
     * @return number
     */
    function getStoreApplyMoney($storeId, $procId){
        $findData = $this->getData(array('storeId'=>$storeId,'procId'=>$procId), 'yearMoney,applyMoney');
        return $findData ? (float)$findData['applyMoney'] : 0;
    }
    /**
     * 根据提供主线ID组装成以id分组的专营店列表
     * @param array $procIds
     * @return array|string
     */
    function getProcStoreListByProcId($procIds){
        $list = $this->getNewList(array('procId'=>array('in', $procIds)));
        $groupByProcIdList = array();
        foreach ($list as $v){
            isset($groupByProcIdList[$v['procId']]) || $groupByProcIdList[$v['procId']] = array();
            $groupByProcIdList[$v['procId']][] = $v;
        }
        return $groupByProcIdList;
    }
}